package homework.day09;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
/**
 * @author ThinkBook
 */
//通过两种方式实现：Comparable接口和Comparator接口
//【要求】
//设计Teacher类，包含属性id，name,age，创建Teacher对象。
//将对象保存在TreeSet集合中并且排序。
//排序规则为：先通过名字排序，名字相同的时候通过年龄进行排序，年龄相同时通过id排序。
public class Test08 {
    public static void main(String[] args) {
        Set<Teacher> set = new TreeSet<>();
        set.add(new Teacher("wjh",1,19));
        set.add(new Teacher("wjh",2,19));
        set.add(new Teacher("zxh",1,20));
        set.add(new Teacher("zui",2,19));
        set.add(new Teacher("syc",3,21));
        set.add(new Teacher("syc",4,22));
        set.add(new Teacher("syc",3,22));

        //迭代器遍历
        Iterator<Teacher> it = set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());

        }
    }

    public static void main01(String[] args) {
        Comparator<Teacher> comp = new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                int r = o1.getName().compareTo(o2.getName());
                if (r == 0){
                    if (o1.getAge() == o2.getAge()){
                        if (o1.getId() == o2.getId()){
                            return 0;
                        }else if (o1.getId()>o2.getId()){
                            return 1;
                        }else {
                            return -1;
                        }
                    }else if (o1.getAge()> o2.getAge()){
                        return 1;
                    }else {
                        return -1;
                    }
                }
                return r;
            }
        };
        Set<Teacher> set = new TreeSet<>(comp);
        set.add(new Teacher("wjh",1,19));
        set.add(new Teacher("wjh",2,19));
        set.add(new Teacher("zxh",1,20));
        set.add(new Teacher("zui",2,19));
        set.add(new Teacher("syc",3,21));
        set.add(new Teacher("syc",4,22));
        set.add(new Teacher("syc",3,22));

        Iterator<Teacher> it = set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());

        }

    }
}
class Teacher implements Comparable{
    private String name;
    private int id;
    private int age;

    public Teacher() {
    }

    public Teacher(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        Teacher t = (Teacher) o;
        int r = this.name.compareTo(t.name);
        if (r ==0){
            r = this.id - t.id;
            if (r==0){
                r = this.age - t.age;
            }
        }
        return r;
    }
}
